﻿using System.Collections.Generic;
using System.Linq;

namespace FlagTunes.Core
{
    public class PlaylistFilter
    {
        /// <summary>
        /// Filters a playlist.
        /// </summary>
        /// <param name="playlist">The playlist.</param>
        /// <param name="searchPattern">The search pattern.</param>
        /// <returns>'A list of the filtered songs</returns>
        public static ICollection<Song> FilterPlaylist(Playlist playlist, string searchPattern)
        {
            if (string.IsNullOrEmpty(searchPattern))
            {
                return playlist.Songs;
            }

            searchPattern = searchPattern.ToLower();

            var keyWords = new List<string>(searchPattern.Split(' '));

            var filteredSongs =
                playlist.Songs.Where(song => keyWords.All(keyWord =>
                    song.Title.ToLower().Contains(keyWord) ||
                    song.Album.ToLower().Contains(keyWord) ||
                    song.Artist.ToLower().Contains(keyWord))).ToList();

            return filteredSongs;
        }

        /// <summary>
        /// Filters a playlist by the specified artist.
        /// </summary>
        /// <param name="playlist">The playlist.</param>
        /// <param name="artist">The artist.</param>
        /// <returns>A list of the filtered songs</returns>
        public static ICollection<Song> FilterPlaylistByArtist(Playlist playlist, string artist)
        {
            return string.IsNullOrEmpty(artist) ? playlist.Songs : playlist.Songs.Where(song => song.Artist == artist).ToList();
        }
    }
}